home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
DEV
/
C-H
/
CMacPrimer.cpt
/
Timer ƒ
/
timer.c
< prev
Wrap
C/C++ Source or Header
|
1990-03-18
|
7KB
|
365 lines
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define MOVE_TO_FRONT -1
#define REMOVE_ALL_EVENTS 0
#define PLAIN 0
#define PLAIN_ITEM 1
#define BOLD_ITEM 2
#define ITALIC_ITEM 3
#define UNDERLINE_ITEM 4
#define OUTLINE_ITEM 5
#define SHADOW_ITEM 6
#define INCLUDE_SECONDS TRUE
#define ADD_CHECK_MARK TRUE
#define REMOVE_CHECK_MARK FALSE
#define DRAG_THRESHOLD 30
#define MIN_SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define WNE_TRAP_NUM 0x60
#define UNIMPL_TRAP_NUM 0x9F
#define QUIT_ITEM 1
#define ABOUT_ITEM 1
#define NOT_A_NORMAL_MENU -1
#define APPLE_MENU_ID BASE_RES_ID
#define FILE_MENU_ID BASE_RES_ID+1
#define FONT_MENU_ID 100
#define STYLE_MENU_ID 101
#define CLOCK_LEFT 12
#define CLOCK_TOP 25
#define CLOCK_SIZE 18
#define ABOUT_ALERT 400
WindowPtr gClockWindow;
Boolean gDone, gWNEImplemented;
long gCurrentTime, gOldTime;
EventRecord gTheEvent;
MenuHandle gAppleMenu, gFontMenu, gStyleMenu;
int gLastFont;
Rect gDragRect;
Style gCurrentStyle = PLAIN;
/**************** main **************************/
main()
{
ToolBoxInit();
WindowInit();
SetUpDragRect();
MenuBarInit();
MainLoop();
}
/********************* ToolBoxInit ******************/
ToolBoxInit()
{
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
InitWindows();
InitMenus();
TEInit();
InitDialogs(NIL_POINTER);
InitCursor();
}
/********************* WindowInit ******************/
WindowInit()
{
gClockWindow = GetNewWindow(BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
SetPort (gClockWindow);
ShowWindow (gClockWindow);
TextSize (CLOCK_SIZE);
}
/******************** SetUpDragRect *******************/
SetUpDragRect()
{
gDragRect = screenBits.bounds;
gDragRect.left += DRAG_THRESHOLD;
gDragRect.right -= DRAG_THRESHOLD;
gDragRect.bottom -= DRAG_THRESHOLD;
}
/************************ MenuBarInit ********************/
MenuBarInit()
{
Handle myMenuBar;
myMenuBar = GetNewMBar (BASE_RES_ID);
SetMenuBar(myMenuBar);
gAppleMenu = GetMHandle (APPLE_MENU_ID);
gFontMenu = GetMenu (FONT_MENU_ID);
gStyleMenu = GetMenu (STYLE_MENU_ID);
InsertMenu (gFontMenu, NOT_A_NORMAL_MENU);
AddResMenu (gFontMenu, 'FONT');
InsertMenu (gStyleMenu, NOT_A_NORMAL_MENU);
CheckItem (gStyleMenu, PLAIN_ITEM, TRUE);
AddResMenu (gAppleMenu, 'DRVR');
DrawMenuBar();
gLastFont = 1;
HandleFontChoice (gLastFont);
}
/******************** MainLoop ***************************/
MainLoop()
{
gDone = FALSE;
gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
NGetTrapAddress (UNIMPL_TRAP_NUM, ToolTrap));
while (gDone == FALSE)
{
HandleEvent();
}
}
/********************** HandleEvent ************************/
HandleEvent ()
{
char theChar;
if (gWNEImplemented)
WaitNextEvent (everyEvent, &gTheEvent, MIN_SLEEP,
NIL_MOUSE_REGION);
else
{
SystemTask();
GetNextEvent(everyEvent,&gTheEvent);
}
switch (gTheEvent.what)
{
case nullEvent:
HandleNull();
break;
case mouseDown:
HandleMouseDown();
break;
case keyDown:
case autoKey:
theChar = gTheEvent.message & charCodeMask;
if (( gTheEvent.modifiers & cmdKey) != 0)
HandleMenuChoice(MenuKey(theChar));
break;
case updateEvt:
BeginUpdate (gTheEvent.message);
EndUpdate(gTheEvent.message);
break;
}
}
/***************** HandleNull **********************/
HandleNull()
{
GetDateTime (&gCurrentTime);
if (gCurrentTime != gOldTime)
{
DrawClock(gClockWindow);
}
}
/********************* DrawClock ***********************/
DrawClock(theWindow)
WindowPtr theWindow;
{
Str255 myTimeString;
IUTimeString (gCurrentTime, INCLUDE_SECONDS, myTimeString);
EraseRect(&(theWindow->portRect));
MoveTo(CLOCK_LEFT, CLOCK_TOP);
DrawString(myTimeString);
gOldTime = gCurrentTime;
}
/************************ HandleMouse ******************/
HandleMouseDown()
{
WindowPtr whichWindow;
short int thePart;
long int menuChoice, windSize;
thePart = FindWindow (gTheEvent.where, &whichWindow);
switch (thePart)
{
case inMenuBar:
menuChoice = MenuSelect (gTheEvent.where);
HandleMenuChoice(menuChoice);
break;
case inSysWindow:
SystemClick (&gTheEvent, whichWindow);
break;
case inDrag:
DragWindow(whichWindow,gTheEvent.where, &gDragRect);
break;
case inGoAway:
gDone = TRUE;
break;
}
}
/***************** HandleMenuChoice ***************/
HandleMenuChoice(menuChoice)
long int menuChoice;
{
int theMenu;
int theItem;
if (menuChoice !=0)
{
theMenu = HiWord (menuChoice);
theItem = LoWord (menuChoice);
switch(theMenu)
{
case APPLE_MENU_ID:
HandleAppleChoice(theItem);
break;
case FILE_MENU_ID:
HandleFileChoice(theItem);
break;
case FONT_MENU_ID:
HandleFontChoice(theItem);
break;
case STYLE_MENU_ID:
HandleStyleChoice(theItem);
break;
}
HiliteMenu(0);
}
}
/********************* HandleAppleChoice *************/
HandleAppleChoice(theItem)
int theItem;
{
Str255 accName;
int accNumber;
short int itemNumber;
DialogPtr AboutDialog;
switch (theItem)
{
case ABOUT_ITEM:
NoteAlert(ABOUT_ALERT, NIL_POINTER);
break;
default:
GetItem(gAppleMenu,theItem,accName);
accNumber = OpenDeskAcc (accName);
break;
}
}
/**************** HandleFileChoice ***********************/
HandleFileChoice (theItem)
int theItem;
{
switch (theItem)
{
case QUIT_ITEM:
gDone = TRUE;
break;
}
}
/********************** HandleFontChoice ******************/
HandleFontChoice (theItem)
int theItem;
{
int fontNumber;
Str255 fontName;
CheckItem (gFontMenu, gLastFont, REMOVE_CHECK_MARK);
CheckItem (gFontMenu, theItem, ADD_CHECK_MARK);
gLastFont = theItem;
GetItem (gFontMenu, theItem, fontName);
GetFNum (fontName,&fontNumber);
TextFont (fontNumber);
}
/************************* HandleStyleChoice *****************/
HandleStyleChoice(theItem)
int theItem;
{
switch(theItem)
{
case PLAIN_ITEM:
gCurrentStyle = PLAIN;
break;
case BOLD_ITEM:
if (gCurrentStyle & bold)
gCurrentStyle -= bold;
else
gCurrentStyle |= bold;
break;
case ITALIC_ITEM:
if (gCurrentStyle & italic)
gCurrentStyle -= italic;
else
gCurrentStyle |= italic;
break;
case UNDERLINE_ITEM:
if (gCurrentStyle & underline)
gCurrentStyle -= underline;
else
gCurrentStyle |= underline;
break;
case OUTLINE_ITEM:
if (gCurrentStyle & outline)
gCurrentStyle -= outline;
else
gCurrentStyle |= outline;
break;
case SHADOW_ITEM:
if (gCurrentStyle & shadow)
gCurrentStyle -= shadow;
else
gCurrentStyle |= shadow;
break;
}
CheckStyles();
TextFace(gCurrentStyle);
}
/************************ CheckStyles ********************/
CheckStyles()
{
CheckItem (gStyleMenu, PLAIN_ITEM, gCurrentStyle == PLAIN);
CheckItem (gStyleMenu, BOLD_ITEM, gCurrentStyle & bold);
CheckItem (gStyleMenu, ITALIC_ITEM, gCurrentStyle & italic);
CheckItem (gStyleMenu, UNDERLINE_ITEM, gCurrentStyle & underline);
CheckItem (gStyleMenu, OUTLINE_ITEM, gCurrentStyle & outline);
CheckItem (gStyleMenu, SHADOW_ITEM, gCurrentStyle & shadow);
}